package com.woniu.rabbit.topic_model.topic_queue_conf;

import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.statement.select.Top;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class TopicExchangeAndQueueConfig {
    public static final String EXCHANGE_NAME = "topic.exchange";
    public static final String QUEUE_NAME01 = "queue.topic01";
    public static final String QUEUE_NAME02 = "queue.topic02";
    public static final String QUEUE_NAME03 = "queue.topic03";

    /**
     * 创建广播交换机
     * */
    @Bean
    public TopicExchange topicExchange(){
        log.info("创建交换机：{}",EXCHANGE_NAME);
        return new TopicExchange(EXCHANGE_NAME,true,false);
    }

    /**
     * 创建 1号队列
     * */
    @Bean
    public Queue topicQueue01(){
        log.info("创建队列：{}",QUEUE_NAME01);
        return new Queue(QUEUE_NAME01,true); // 指定队列名称,持久化
    }
    /**
     * 创建 2号队列
     * */
    @Bean
    public Queue topicQueue02(){
        log.info("创建队列：{}",QUEUE_NAME02);
        return new Queue(QUEUE_NAME02,true);
    }
    /**
     * 创建 3号队列
     * */
    @Bean
    public Queue topicQueue03(){
        log.info("创建队列：{}",QUEUE_NAME03);
        return new Queue(QUEUE_NAME03,true);
    }

    /**
     * 将队列绑定到交换机
     * */
    @Bean
    public Binding TopicBinding01(TopicExchange topicExchange, Queue topicQueue01){
        // `#`：匹配一个或多个单词（例如 `*.stock.*` 匹配 `us.stock.orcl`、`us.stock.ibm`）。
        return BindingBuilder.bind(topicQueue01).to(topicExchange).with("china.#");
    }

    @Bean
    public Binding TopicBinding02(TopicExchange topicExchange,Queue topicQueue02){
        // `*`：匹配一个单词（例如 `key.*` 匹配 `key.a`、`key.b`）。
        return BindingBuilder.bind(topicQueue02).to(topicExchange).with("*.news");
    }

}
